package com.hilinos.common.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    private static final SimpleDateFormat SIMPLE_DATE_FORMAT_YMDHMS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static final SimpleDateFormat SIMPLE_DATE_FORMAT_YMD = new SimpleDateFormat("yyyy-MM-dd");
    private static final long KEY_YMD = 0;
    private static final long DAY_MILL_NUM = 86400000;
    private static long KEY = 0;
    private static String DATE_STRING = null;
    private static String DATE_STRING_YMD = null;

    /**
     * 使用parrern规则生成日期字符串
     *
     * @param date
     * @return
     */
    public static synchronized Date ymdStringToDate(String date) {
        try {
            return SIMPLE_DATE_FORMAT_YMD.parse(date);
        } catch (Exception ignored) {
        }
        return null;
    }

    /**
     * 使用parrern规则生成日期字符串
     *
     * @param date
     * @return
     */
    public static synchronized Date ymdhmsStringToDate(String date) {
        try {
            return SIMPLE_DATE_FORMAT_YMDHMS.parse(date);
        } catch (Exception ignored) {
        }
        return null;
    }

    /**
     * 使用parrern规则生成日期字符串
     *
     * @param pattern
     * @return
     */
    public static synchronized String getDateString(String pattern) {
        return getDateString(new Date(), pattern);
    }

    /**
     * 自定义日期使用parrern规则生成日期字符串
     *
     * @param date
     * @param pattern
     * @return
     */
    public static synchronized String getDateString(Date date, String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }

    /**
     * 使用生成年月日时分秒时间字符串，规则为："yyyy-MM-dd HH:mm:ss"
     *
     * @param date
     * @return
     */
    public synchronized static String getDateString(Date date) {
        if (date.getTime() / 1000 == KEY) {
            return DATE_STRING;
        }
        DATE_STRING = SIMPLE_DATE_FORMAT_YMDHMS.format(date);
        return DATE_STRING;
    }

    /**
     * 使用当前时间生成年月日时分秒时间字符串，规则为："yyyy-MM-dd HH:mm:ss"
     *
     * @return
     */
    public static String getDateString() {
        return getDateString(new Date());
    }

    /**
     * 使用生成年月日日期字符串，规则为："yyyy-MM-dd"
     *
     * @param date
     * @return
     */
    public synchronized static String getDateStringYmd(Date date) {
        if (date.getTime() / DAY_MILL_NUM == KEY_YMD) {
            return DATE_STRING_YMD;
        }
        DATE_STRING_YMD = SIMPLE_DATE_FORMAT_YMD.format(date);
        return DATE_STRING_YMD;
    }

    /**
     * 使用当前时间生成年月日时间字符串，规则为："yyyy-MM-dd"
     *
     * @return
     */
    public static String getDateStringYmd() {
        return getDateStringYmd(new Date());
    }

    /**
     * 计算两个时间点间的周数
     *
     * @param start
     * @param end
     * @return
     */
    public static int getBetweenWeek(Date start, Date end) {
        Calendar cStart = Calendar.getInstance();
        cStart.setTime(start);
        Calendar cEnd = Calendar.getInstance();
        cEnd.setTime(end);
        // 星期几
        int cStartDayOfWeek = (cStart.get(Calendar.DAY_OF_WEEK) - 1) == 0 ? 7 : (cStart.get(Calendar.DAY_OF_WEEK) - 1);
        long l = cEnd.getTime().getTime() - (cStart.getTime().getTime() - cStartDayOfWeek * 24 * 60 * 60 * 1000) - 1;
        long djz = l / (24 * 60 * 60 * 1000 * 7) + 1;
        return (int) djz;
    }

    /**
     * 秒数转换为时间文本，如2天3小时5分钟2秒
     *
     * @param mss 秒数
     * @return
     */
    public static String formatDateTime(long mss) {
        String dateTimes = null;
        long days = mss / (60 * 60 * 24);
        long hours = (mss % (60 * 60 * 24)) / (60 * 60);
        long minutes = (mss % (60 * 60)) / 60;
        long seconds = mss % 60;
        if (days > 0) {
            dateTimes = days + "天" + hours + "小时" + minutes + "分钟"
                    + seconds + "秒";
        } else if (hours > 0) {
            dateTimes = hours + "小时" + minutes + "分钟"
                    + seconds + "秒";
        } else if (minutes > 0) {
            dateTimes = minutes + "分钟"
                    + seconds + "秒";
        } else {
            dateTimes = seconds + "秒";
        }
        return dateTimes;
    }
}
